package com.example.yan_music.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.yan_music.R;
import com.example.yan_music.db.table.HistoryRecord;
import com.example.yan_music.presenter.ISearchPresenterImp;

import java.util.ArrayList;
import java.util.List;

public class RcvHistoryAdapter extends RecyclerView.Adapter<RcvHistoryAdapter.MyHolder> {

    private static final String TAG = "RcvHistoryAdapter";

    private Context mContext;
    private ISearchPresenterImp iSearchPresenterImp;
    private List<HistoryRecord> mHistoryList = new ArrayList<>();

    public RcvHistoryAdapter(Context mContext, List<HistoryRecord> mHistoryList, ISearchPresenterImp iSearchPresenterImp) {
        this.mContext = mContext;
        this.mHistoryList = mHistoryList;
        this.iSearchPresenterImp = iSearchPresenterImp;
    }

    @NonNull
    @Override
    public MyHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.rcv_history_item, null);
            return new MyHolder(view);

    }

    @Override
    public void onBindViewHolder(@NonNull MyHolder holder, int position) {
         holder.setData(position);

    }

    @Override
    public int getItemCount() {
        return mHistoryList.size();
    }


    /**
     * ViewHolder
     */
    class MyHolder extends RecyclerView.ViewHolder {
        private TextView tv_record;
        // 字段值，这个值是从历史记录表中取出的数据
        private String historyRecord;

        public MyHolder(@NonNull View itemView) {
            super(itemView);
            tv_record = itemView.findViewById(R.id.tv_his_name);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //ToastUtil.showToast(mContext, tv_record.getText().toString());
                    iSearchPresenterImp.search(historyRecord, 1);
                }
            });
        }

        public void setData(int position) {
            historyRecord = mHistoryList.get(position).getRecordName();
            tv_record.setText(historyRecord);
            Log.d(TAG, "setData: " + "当前子项的位置position = [ " + position + " ]，给子项控件TV设的字段值为：[ " + historyRecord + " ]");
        }
    }
}
